package com.demo.okhttp;

import com.alibaba.fastjson.JSONObject;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;

public class OkHttpPostDemo {
    public static void main(String[] args) throws IOException {
        String url = "http://gadmin-pp.weimipopo.com/user/login";

        //1 创建okhttpclient请求对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //2 创建requestbody
        MediaType type = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody requestBody = RequestBody.create(type, "username=admin&password=123456");
        Request request = new Request.Builder().url(url).post(requestBody).build();

        //发情请求
        Response execute = okHttpClient.newCall(request).execute();
        System.out.println(execute.code());
        System.out.println(execute.headers());
        System.out.println(JSONObject.toJSONString(execute));

    }
}
